# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Naveen M K <naveen521kk@gmail.com>

_realname=inkscape
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.2
pkgrel=1
_pkg_suffix=2022-02-04_0a00cf5339
pkgdesc="Vector graphics editor using the SVG file format (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://inkscape.sourceforge.io/"
license=("GPL" "LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "intltool")
depends=("${MINGW_PACKAGE_PREFIX}-aspell"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-gc"
         "${MINGW_PACKAGE_PREFIX}-gdl"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-gtkmm3"
         "${MINGW_PACKAGE_PREFIX}-gtkspell3"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-imagemagick"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-lib2geom"
         "${MINGW_PACKAGE_PREFIX}-libcdr"
         "${MINGW_PACKAGE_PREFIX}-librevenge"
         "${MINGW_PACKAGE_PREFIX}-libsoup"
         "${MINGW_PACKAGE_PREFIX}-libvisio"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-libwpg"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-popt"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-scour"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] && echo "${MINGW_PACKAGE_PREFIX}-openmp"))
optdepends=(#"${MINGW_PACKAGE_PREFIX}-pstoedit: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-texlive-scheme-medium: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-python-numpy: some extensions"
            "${MINGW_PACKAGE_PREFIX}-python-lxml: some extensions and filters"
            #"${MINGW_PACKAGE_PREFIX}-scour: optimized SVG output"
            #"${MINGW_PACKAGE_PREFIX}-uniconvertor: reading/writing to some proprietary formats"
            )
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.xz::https://media.inkscape.org/dl/resources/file/${_realname}-${pkgver}.tar.xz"
        inkscape-1.0.1-unbundle.patch
        inkscape-1.0.1-install-layout.patch
        3687-clang-fix.patch
        3688-clang-fix-2.patch
        clang-fix-3.patch
        clang-fix-4.patch)
sha256sums=('3ffe54a06d0b25a4cd8b6eb424536ef1ed205be13443a39cd437c8c7b89b96d1'
            '92387251c1740f1a57cde9e587cc673ef1600813617bd2c9db65598c324a24d4'
            '131b2e1190637df0554ef1ee8cf46440689584375c117d057ab47d5871c58128'
            'e2bf5379e049570428a6cb242f1ecc451ff9e34432449e5961325a9e75d39e0d'
            '0b90a9a85ca6bf226e03bb8051eb6426bb9b8cba7d7d8e51902569d17fafedb3'
            '26434649cc12d9005a1f8b1deaae2decc3acd720a7bee7418a052e6275833b44'
            'b10f64aa50704bfa1f89a1559d3a1471f199ba833c708668c5290d5d68c340ae')

prepare() {
  mv "${srcdir}"/${_realname}-${pkgver}_${_pkg_suffix} "${srcdir}"/${_realname}-${pkgver}
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i ${srcdir}/inkscape-1.0.1-unbundle.patch
  patch -p1 -i ${srcdir}/inkscape-1.0.1-install-layout.patch

  # https://gitlab.com/inkscape/inkscape/-/merge_requests/3687
  patch -p1 -i ${srcdir}/3687-clang-fix.patch

  # https://gitlab.com/inkscape/inkscape/-/merge_requests/3688
  patch -p1 -i ${srcdir}/3688-clang-fix-2.patch

  patch -p1 -i ${srcdir}/clang-fix-3.patch
  patch -p1 -i ${srcdir}/clang-fix-4.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  MSYS=winsymlinks:native \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DBUILD_TESTING=OFF \
    -DCMAKE_AR=${MINGW_PREFIX}/bin/ar \
    -DWITH_IMAGE_MAGICK=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
